package com.woniuxy.movie.cinema.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.movie.cinema.dto.seat.SeatDto;
import com.woniuxy.movie.cinema.form.cinemaseat.SelectHallSeatForm;
import com.woniuxy.movie.cinema.form.cinemaseat.updateSeatStateForm;
import com.woniuxy.movie.cinema.model.CinemaSeat;
import com.woniuxy.movie.cinema.service.CinemaSeatService;
import com.woniuxy.movie.commons.core.model.Result;
import com.woniuxy.movie.commons.web.util.StateData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 影厅座位详情信息 前端控制器
 * </p>
 *
 * @author 刘存益
 * @since 2023年11月17日
 */
@RestController
@RequestMapping("/cinemaSeat")
@Api(tags = "影厅座位相关接口")
@Slf4j
public class CinemaSeatController {

    @Autowired
    private CinemaSeatService cinemaSeatService;

    @GetMapping("/selNormal.api")
    @ApiOperation("查询影厅的可用的座位")
    public Result<List<SeatDto>> selNormal(Integer hallId){
        List<CinemaSeat> cinemaSeats = cinemaSeatService.selNormal(hallId);
        List<SeatDto> seatDtos = BeanUtil.copyToList(cinemaSeats, SeatDto.class);
        return Result.getListResult(SeatDto.class).setData(seatDtos).setMsg("查询影厅的可用座位成功");
    }

    @PostMapping("/updateSeatState.api")
    @ApiOperation("修改影厅座位状态")
    public Result<String> updateSeatState(@RequestBody updateSeatStateForm form){

        cinemaSeatService.updateSeatState(form);

        return Result.getResult(String.class).setMsg("修改影厅座位状态成功");
    }

    @PostMapping("/select")
    @ApiOperation("查询影厅的座位")
    public Result<List<CinemaSeat>> selectHallSeat(@RequestBody SelectHallSeatForm form){
        List<CinemaSeat> cinemaSeats = cinemaSeatService.selectHallSeat(form);
        return Result.getListResult(CinemaSeat.class).setData(cinemaSeats);
    }

    @PostMapping("/byNormalIds.api")
    @ApiOperation("查询多个影厅的可用的座位")
    public Result<List<SeatDto>> byNormalIds(@RequestBody Set<Integer> ids){
        log.info("========开始执行座位查询操作：{}===========",ids);
        Long start = System.currentTimeMillis();
        List<CinemaSeat> cinemaSeats = cinemaSeatService.list(new QueryWrapper<CinemaSeat>().in("seat_id",ids).eq("seat_state", StateData.SEAT_STATE_NORMAL));
        List<SeatDto> seatDtos = BeanUtil.copyToList(cinemaSeats, SeatDto.class);
        log.info("==========查询操作结束，耗时：{}，结果是：{}========",System.currentTimeMillis()-start, JSONUtil.toJsonStr(seatDtos));
        return Result.getListResult(SeatDto.class).setData(seatDtos).setMsg("查询影厅的可用座位成功");
    }


}

